<?php
/**
 * Created by PhpStorm.
 * User: 技术员专用
 * Date: 2019/3/1
 * Time: 9:33
 */

namespace app\admin\controller;

use app\admin\model\Groups as GroupsModel;
use think\Db;
class Groups extends Permissions
{
    public function index(){
        $post = $this->request->param();
        if (isset($post['remarkname']) and !empty($post['remarkname'])) {
            $user_id=db('users')->where(['remarkname'=>$post['remarkname']])->value('id');
            $where['users_id'] = $user_id;
        }
        if (isset($post['group']) and !empty($post['group'])) {
            $where['group'] = ['like', '%' . $post['group'] . '%'];
        }
        if(isset($post['day']) and !empty($post['day'])) {
            $where['day'] = $post['day'];
        }
        $model=new GroupsModel();
        $group=empty($where)?$model->where(['status'=>0])->paginate(20):$model->where(['status'=>0])->where($where)->paginate(20);
        $this->assign('group',$group);
        return $this->fetch();
    }
    public function check(){
        $post = $this->request->param();
        if (isset($post['remarkname']) and !empty($post['remarkname'])) {
            $user_id=db('users')->where(['remarkname'=>$post['remarkname']])->value('id');
            $where['users_id'] = $user_id;
        }
        if (isset($post['group']) and !empty($post['group'])) {
            $where['group'] = ['like', '%' . $post['group'] . '%'];
        }
        if(isset($post['day']) and !empty($post['day'])) {
            $where['day'] = $post['day'];
        }
        if(isset($post['status']) and !empty($post['status'])) {
            $where['status'] = $post['status'];
        }
        $model=new GroupsModel();
        $group=empty($where)?$model->where('status','<>',0)->paginate(20):$model->where('status','<>',0)->where($where)->paginate(20);
        $this->assign('group',$group);
        return $this->fetch();
    }
    public function edit(){
        if (request()->isPost()){
            $post=input('post.');
            if (db('groups')->update($post)===false){
                return $this->error('修改失败');
            }else{
                return $this->success('更新成功','admin/groups/index');
            }
        }
        $id=input('id');
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function agree(){
        $id=input('id');
        $res=db('groups')->update(['id'=>$id,'status'=>1]);
        if ($res){
            $user_id=db('groups')->find($id);
            $user=db('users')->find($user_id['users_id']);
            $user['money']=$user['money']+$user_id['money'];
            db('users')->update($user);
            return $this->success('同意成功','admin/groups/index');
        }else{
            return $this->error('同意失败','admin/groups/index');
        }
    }

    public function reject(){
        $id=input('id');
        $res=db('groups')->update(['id'=>$id,'status'=>2]);
        if ($res){
            return $this->success('拒绝成功','admin/groups/index');
        }else{
            return $this->error('拒绝失败','admin/groups/index');
        }
    }

    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('groups')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
//                addlog($id);//写入日志
                return $this->success('删除成功','admin/groups/index');
            }
        }
    }

}